package com.ruoyi.map.userController;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.chat.domain.FhChatFriend;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.index.domain.FhIndexPostinfo;
import com.ruoyi.index.domain.WeatherDto;
import com.ruoyi.index.service.IFhIndexPostinfoService;
import com.ruoyi.index.service.IWeatherService;
import com.ruoyi.info.domain.FhInfoCatchrecord;
import com.ruoyi.info.domain.FhInfoFishpoint;
import com.ruoyi.info.mapper.FhInfoEquipMapper;
import com.ruoyi.info.service.IFhInfoCatchrecordService;
import com.ruoyi.info.service.IFhInfoFishpointService;
import com.ruoyi.map.domain.*;
import com.ruoyi.map.service.*;
import com.ruoyi.tokenTools.UserLoginToken;
import com.ruoyi.web.core.config.LocalUserInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.math3.random.RandomDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * 地图模块接口管理Controller
 *
 * @author ruoyi
 * @date 2024-11-12
 */
@Api(tags = "地图模块接口")
@RestController
@RequestMapping("/app/map")
public class UserFhMapController extends BaseController {

    @Autowired
    private IFhIndexPostinfoService iFhIndexPostinfoService;

    @Autowired
    private IFhMapFishtypeService fhMapFishtypeService;

    @Autowired
    private IFhMapBaitrecommendService iFhMapBaitrecommendService;

    @Autowired
    private IFhMapFishbaitService fhMapFishbaitService;

    @Autowired
    private IFhInfoFishpointService fhInfoFishpointService;

    @Autowired
    private IFhMapWaterService fhMapWaterService;

    @Autowired
    private IFhMapWatercommonfishService fhMapWatercommonfishService;

    @Autowired
    private IFhMapFishprotectionService fhMapFishprotectionService;

    @Autowired
    private IWeatherService weatherService;

    @Autowired
    private IFhMapGuideService fhMapGuideService;

    @Autowired
    private FhInfoEquipMapper fhInfoEquipMapper;

    @Autowired
    private IFhInfoCatchrecordService fhInfoCatchrecordService;

    @Autowired
    private IH5ArtUserFriendService h5ArtUserFriendService;

    @Autowired
    private IFhInviteRecordService fhInviteRecordServicel;

    /**
     * 查询附进渔获
     */

    @UserLoginToken
    @ApiOperation("附近渔获")
    @GetMapping("/nearbyCatch")
    public R<ArrayList<String>> nearbyCatch(String longitude, String latitude) {
        try {
            FhIndexPostinfo fhIndexPostinfo = new FhIndexPostinfo();
            fhIndexPostinfo.setLongitude(longitude);
            fhIndexPostinfo.setLatitude(latitude);
            List<FhIndexPostinfo> fhIndexPostinfoList = iFhIndexPostinfoService.selectFhIndexPostinfoList(fhIndexPostinfo);
            ArrayList<String> nearbyCatchImageList = new ArrayList<>();
            if (fhIndexPostinfoList.isEmpty()) {
                return R.ok(nearbyCatchImageList);
            }
            for (FhIndexPostinfo indexPostinfo : fhIndexPostinfoList) {
                String postImages = indexPostinfo.getPostImages();
                String[] postImageList = postImages.split(",");
                nearbyCatchImageList.addAll(Arrays.asList(postImageList));
            }
            return R.ok(nearbyCatchImageList);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /**
     * 查询附近水域鱼类
     */
    @UserLoginToken
    @ApiOperation("附近水域鱼类")
    @GetMapping("/nearbyWaterFish")
    public R<List<NearbyFishType>> nearbyWaterFish(String longitude, String latitude, String waterType) {
//        startPage();
        //返回参数，附近水域鱼种类
        List<NearbyFishType> nearbyFishTypeList = new ArrayList<>();

        //查询附近水域。将附近水域的常见鱼种添加到返回数据中
        FhMapWater fhMapWater = new FhMapWater();
        fhMapWater.setLongitude(longitude);
        fhMapWater.setLatitude(latitude);
        if (StringUtils.isNotEmpty(waterType)) {
            fhMapWater.setWaterType(waterType);
        }
        List<FhMapWater> fhMapWaters = fhMapWaterService.selectFhMapWaterList(fhMapWater);
        //附近没有水域
        if (fhMapWaters.isEmpty()) {
            return R.ok(nearbyFishTypeList);
        }
        //附近存在水域
        FhMapWater fhMapWaterInfo = fhMapWaters.get(0);
        Long waterId = fhMapWaterInfo.getId();
        List<FhMapWatercommonfish> fhMapWatercommonfishList = fhMapWatercommonfishService.selectMapWatercommonfishListByWaterId(waterId);

        for (int i = 0; i < fhMapWatercommonfishList.size(); i++) {

            FhMapWatercommonfish fhMapWatercommonfishInfo = fhMapWatercommonfishList.get(i);

            NearbyFishType nearbyFishType = new NearbyFishType();
            nearbyFishType.setFishId(fhMapWatercommonfishInfo.getFishId());
            nearbyFishType.setFishType(fhMapWatercommonfishInfo.getFishName());
            nearbyFishType.setFishImage(fhMapWatercommonfishInfo.getFishImage());
            nearbyFishType.setCatch_num(fhMapWatercommonfishInfo.getCatchNum());
//
//            FhMapFishtype fhMapFishtype = new FhMapFishtype();
//            fhMapFishtype.setId(fhMapWatercommonfishInfo.getFishId());
//            List<FhMapFishtype> fhMapFishtypes = fhMapFishtypeService.selectFhMapFishtypeList(fhMapFishtype);
//            if (fhMapFishtypes.isEmpty()) {
//                return R.ok(nearbyFishTypeList);
//            }
//            FhMapFishtype fhMapFishtypeInfo = fhMapFishtypes.get(0);
//            String baitIds = fhMapFishtypeInfo.getBaitIds();
//            String[] baitIdList = baitIds.split(",");


            FhMapFishtype fhMapFishtype = fhMapFishtypeService.selectFhMapFishtypeById(fhMapWatercommonfishInfo.getFishId());
            //获取鱼饵id
            String baitIds = fhMapFishtype.getBaitIds();
            //分割字符串为ID列表
            List<Long> baitIdList = Arrays.stream(baitIds.split(","))
                    .map(String::trim)  // 去除空格
                    .filter(s -> !s.isEmpty())  // 过滤空值
                    .map(Long::parseLong)  // 转为Long类型
                    .collect(Collectors.toList());
            //根据鱼饵id查询鱼饵信息
            List<FhMapFishbait> fhMapBaitrecommends = fhMapFishbaitService.selectList(baitIdList);
//
//
//
//
//            List<FhMapFishbait> fhMapBaitrecommends = new ArrayList<>();
//            for (int j = 0; j < baitIdList.length; j++) {
//                Long baitId = Long.parseLong(baitIdList[j]);
//                FhMapFishbait fhMapFishbait = fhMapFishbaitService.selectFhMapFishbaitById(baitId);
////                //如果有则添加
//                if (fhMapFishbait != null) {
//                    fhMapBaitrecommends.add(fhMapFishbait);
//                }
//                //fhMapBaitrecommends.add(fhMapFishbait);
//            }

            nearbyFishType.setFhMapBaitrecommendList(fhMapBaitrecommends);

            nearbyFishTypeList.add(nearbyFishType);
        }

        return R.ok(nearbyFishTypeList);
    }


    /**
     * 查询附近注意事项
     */
    @UserLoginToken
    @ApiOperation("附近注意事项")
    @GetMapping("/nearbyAttention")
    public R<FhMapWaterDto> nearbyAttention(String longitude, String latitude) {
        try {
            FhMapWaterDto nearbyAttentionDto = new FhMapWaterDto();
            FhMapWater fhMapWater = new FhMapWater();
            fhMapWater.setLongitude(longitude);
            fhMapWater.setLatitude(latitude);
            List<FhMapWater> fhMapWaters = fhMapWaterService.selectFhMapWaterList(fhMapWater);
            //附近没有水域
            if (fhMapWaters.isEmpty()) {
                return R.ok(nearbyAttentionDto);
            } else {
                for (int j = 0; j < fhMapWaters.size(); j++) {
                    List<FhMapFishtype> fhMapFishprotections = new ArrayList<>();
                    FhMapWater fhMapWaterInfo = fhMapWaters.get(j);
                    if (!StringUtils.isEmpty(fhMapWaterInfo.getProtectingFishids())){
                        String id = fhMapWaterInfo.getProtectingFishids();
                        String[] fishIdList = id.toString().split(",");
                        //循环上面的fishIdList
                        for (int i = 0; i < fishIdList.length; i++) {
                            Long fishId = Long.parseLong(fishIdList[i]);
                            FhMapFishtype fhMapFishtype = fhMapFishtypeService.selectFhMapFishtypeById(fishId);
                            if (fhMapFishtype != null) {
                                fhMapFishprotections.add(fhMapFishtype);
                            }
                        }
                    }

                    nearbyAttentionDto.setId(fhMapWaters.get(j).getId());
                    nearbyAttentionDto.setWaterName(fhMapWaters.get(j).getWaterName());
                    nearbyAttentionDto.setWaterType(fhMapWaters.get(j).getWaterType());
                    nearbyAttentionDto.setLocationName(fhMapWaters.get(j).getLocationName());
                    nearbyAttentionDto.setLongitude(fhMapWaters.get(j).getLongitude());
                    nearbyAttentionDto.setLatitude(fhMapWaters.get(j).getLatitude());
                    nearbyAttentionDto.setCatchTotal(fhMapWaters.get(j).getCatchTotal());
                    nearbyAttentionDto.setPrice(fhMapWaters.get(j).getPrice());
                    nearbyAttentionDto.setUseStatus(fhMapWaters.get(j).getUseStatus());
                    nearbyAttentionDto.setProtectingFishids(fhMapWaters.get(j).getProtectingFishids());
                    nearbyAttentionDto.setFishingZone(fhMapWaters.get(j).getFishingZone());
                    nearbyAttentionDto.setNoFishingZone(fhMapWaters.get(j).getNoFishingZone());
                    nearbyAttentionDto.setLawsRegulations(fhMapWaters.get(j).getLawsRegulations());
                    nearbyAttentionDto.setImage(fhMapWaters.get(j).getImage());
                    nearbyAttentionDto.setFhMapFishtypeList(fhMapFishprotections);
                }
            }
            return R.ok(nearbyAttentionDto);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }


    /**
     * 获取鱼类管理详细信息
     */
    @UserLoginToken
    @ApiOperation("鱼类详情鱼种百科")
    @GetMapping(value = "/fishtype/{fishId}")
    public R<FhMapFishtypeDto> getFishInfo(@PathVariable("fishId") Long fishId) {
        try {
            //获取垂钓指南
            List<FhMapGuide> fhMapGuides = fhMapGuideService.guideList(fishId);

            FhMapFishtypeDto fhMapFishtype = fhMapFishtypeService.selectFhMapFishtypeById(fishId);
            fhMapFishtype.setFishtypes(fhMapGuides);
            return R.ok(fhMapFishtype);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取鱼饵推荐
     */
    @UserLoginToken
    @ApiOperation("鱼类详情鱼饵推荐")
    @GetMapping("/fhMapFishbait")
    public R<List<FhMapFishbait>> fhMapFishbait(Long fishId) {
        try {
            //根据鱼类id查询鱼类信息
            FhMapFishtype fhMapFishtype = fhMapFishtypeService.selectFhMapFishtypeById(fishId);
            //获取鱼饵id
            String baitIds = fhMapFishtype.getBaitIds();
            //分割字符串为ID列表
            List<Long> baitIdList = Arrays.stream(baitIds.split(","))
                    .map(String::trim)  // 去除空格
                    .filter(s -> !s.isEmpty())  // 过滤空值
                    .map(Long::parseLong)  // 转为Long类型
                    .collect(Collectors.toList());
            //根据鱼饵id查询鱼饵信息
            List<FhMapFishbait> baitList = fhMapFishbaitService.selectList(baitIdList);
            return R.ok(baitList);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取鱼类详情最近渔获
     */
    @UserLoginToken
    @ApiOperation("鱼类详情最近渔获")
    @GetMapping("/recentCatch")
    public R<List<Map<String, Object>>> recentCatch(Long fishId) {
        try {
            //返回数据,鱼类最近渔获图片
            List<Map<String, Object>> recentCatchList = new ArrayList<>();

            FhIndexPostinfo query = new FhIndexPostinfo();
            List<FhIndexPostinfo> postInfos = iFhIndexPostinfoService.selectFhIndexPostinfoList(query);


            for (FhIndexPostinfo post : postInfos) {
                // 安全获取 fishIds 并判断是否匹配
                String fishIdsStr = post.getFishIds();
                if (StringUtils.isEmpty(fishIdsStr)) {
                    continue; // 跳过空值
                }
                String[] fishIds = fishIdsStr.split(",");
                if (fishIds.length != 1 || Long.parseLong(fishIds[0]) != fishId) {
                    continue; // 不匹配或格式不正确
                }

                // 安全获取图片 URL
                String postImagesStr = post.getPostImages();
                if (StringUtils.isEmpty(postImagesStr)) {
                    continue; // 没有图片，跳过
                }
                String[] images = postImagesStr.split(",");

                // 构建返回对象
                for (String imageUrl : images) {
                    Map<String, Object> item = new HashMap<>();
                    item.put("postId", post.getId());     // 帖子ID
                    item.put("imageUrl", imageUrl);       // 图片URL
                    recentCatchList.add(item);
                }
            }
            return R.ok(recentCatchList);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取鱼饵详细信息
     */
    @UserLoginToken
    @ApiOperation("鱼饵推荐详情")
    @GetMapping(value = "/fishBait/{baitId}")
    public R<FhMapFishbait> getFishBaitInfo(@PathVariable("baitId") Long id) {
        try {
            FhMapFishbait fhMapFishbait = fhMapFishbaitService.selectFhMapFishbaitById(id);
            if (!StringUtils.isEmpty(fhMapFishbait.getVarietyIds())){
                String[] fishIds = fhMapFishbait.getVarietyIds().split(",");
                //获取变种鱼饵
                List<FhMapFishbait> varietyFishBait = fhMapFishbaitService.getVarietyFishBait(fishIds);

                fhMapFishbait.setVariationFishList(varietyFishBait);
            }
            return R.ok(fhMapFishbait);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /**
     * 水域详情
     */
    @UserLoginToken
    @ApiOperation("水域详情")
    @GetMapping(value = "/water/{waterId}")
    public R<WaterInfoDto> getWaterInfo(@PathVariable("waterId") Long waterId) {
        try {
            WaterInfoDto waterInfoDto = new WaterInfoDto();

            //水域详情
            FhMapWater fhMapWater = fhMapWaterService.selectFhMapWaterById(waterId);
            waterInfoDto.setFhMapWater(fhMapWater);

            //常见鱼种
            FhMapWatercommonfish fhMapWatercommonfish = new FhMapWatercommonfish();
            fhMapWatercommonfish.setWaterId(waterId);
            List<FhMapWatercommonfish> fhMapWatercommonfishList = fhMapWatercommonfishService.commonfishlist(fhMapWatercommonfish);
            waterInfoDto.setFhMapWatercommonfishList(fhMapWatercommonfishList);

            //获取天气
            WeatherDto weatherData = weatherService.getWeatherData(fhMapWater.getLatitude(), fhMapWater.getLongitude());
            waterInfoDto.setWeatherDto(weatherData);

            //鱼获预测
            //如果isopen为1则直接使用catchredata中的数据他是这样的22,52,44,56,77,19,54,44,23,4,5,7,8,9,3,4,56,6,7,31,31,31,41,65，帮我转成这个实体中需要的
            if ("1".equals(fhMapWater.getIsopen())) {
                String[] catchredata = fhMapWater.getCatchredata().split(",");
                // 确保数据长度正确（24小时数据）
                if (catchredata.length >= 24) {
                    FhInfoCatchrecordVO fhInfoCatchrecord = new FhInfoCatchrecordVO();
                    fhInfoCatchrecord.setOne(catchredata[0]);
                    fhInfoCatchrecord.setTwo(catchredata[1]);
                    fhInfoCatchrecord.setThree(catchredata[2]);
                    fhInfoCatchrecord.setFour(catchredata[3]);
                    fhInfoCatchrecord.setFive(catchredata[4]);
                    fhInfoCatchrecord.setSix(catchredata[5]);
                    fhInfoCatchrecord.setSeven(catchredata[6]);
                    fhInfoCatchrecord.setEight(catchredata[7]);
                    fhInfoCatchrecord.setNine(catchredata[8]);
                    fhInfoCatchrecord.setTen(catchredata[9]);
                    fhInfoCatchrecord.setEleven(catchredata[10]);
                    fhInfoCatchrecord.setTwelve(catchredata[11]);
                    fhInfoCatchrecord.setThirteen(catchredata[12]);
                    fhInfoCatchrecord.setFourteen(catchredata[13]);
                    fhInfoCatchrecord.setFifteen(catchredata[14]);
                    fhInfoCatchrecord.setSixteen(catchredata[15]);
                    fhInfoCatchrecord.setSeventeen(catchredata[16]);
                    fhInfoCatchrecord.setEighteen(catchredata[17]);
                    fhInfoCatchrecord.setNineteen(catchredata[18]);
                    fhInfoCatchrecord.setTwenty(catchredata[19]);
                    fhInfoCatchrecord.setTwentyOne(catchredata[20]);
                    fhInfoCatchrecord.setTwentyTwo(catchredata[21]);
                    fhInfoCatchrecord.setTwentyThree(catchredata[22]);
                    fhInfoCatchrecord.setTwentyFour(catchredata[23]);

                    waterInfoDto.setFhInfoCatchrecordVO(fhInfoCatchrecord);
                }
            }else{
                FhInfoCatchrecordVO fhMapWaterVO = fhInfoCatchrecordService.getFishForecasting();
                waterInfoDto.setFhInfoCatchrecordVO(fhMapWaterVO);
            }


            //最近渔获照片
            List<Map<String, Object>> catchImageList = new ArrayList<>();
            FhIndexPostinfo fhIndexPostinfo = new FhIndexPostinfo();
            fhIndexPostinfo.setLongitude(fhMapWater.getLongitude());
            fhIndexPostinfo.setLatitude(fhMapWater.getLatitude());
            List<FhIndexPostinfo> fhIndexPostinfos = iFhIndexPostinfoService.selectFhIndexPostinfoList(fhIndexPostinfo);

            for (FhIndexPostinfo postInfo : fhIndexPostinfos) {
                if (postInfo.getPostImages() == null || postInfo.getPostImages().isEmpty()) {
                    continue;
                }

                // 分割图片URL，并为每张图片关联帖子ID
                String[] images = postInfo.getPostImages().split(",");
                for (String imageUrl : images) {
                    Map<String, Object> imageMap = new HashMap<>();
                    imageMap.put("postId", postInfo.getId());  // 动态ID
                    imageMap.put("imageUrl", imageUrl);       // 图片URL
                    catchImageList.add(imageMap);
                }
            }

            waterInfoDto.setCatchImageMap(catchImageList);  // 需修改WaterInfoDto中字段类型

            return R.ok(waterInfoDto);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }


    /**
     * 搜素水域
     */
    @UserLoginToken
    @ApiOperation("搜素水域")
    @GetMapping("/searchWater")
    public R<List<FhMapWater>> searchWater(String waterName) {
        try {
            FhMapWater fhMapWater = new FhMapWater();
            fhMapWater.setWaterName(waterName);
            List<FhMapWater> fhMapWaters = fhMapWaterService.selectFhMapWaterList(fhMapWater);
            return R.ok(fhMapWaters);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /**
     * 附近水域
     */
    @UserLoginToken
    @ApiOperation("附近水域")
    @GetMapping("/nearyWater")
    public R<List<FhMapWater>> nearbyWater(String longitude, String latitude, String waterType, String waterName,String scope) {
        try {
            FhMapWater fhMapWater = new FhMapWater();
            fhMapWater.setLongitude(longitude);
            fhMapWater.setLatitude(latitude);
            fhMapWater.setWaterType(waterType);
            fhMapWater.setWaterName(waterName);
            List<FhMapWater> fhMapWaters = fhMapWaterService.selectWaterList(fhMapWater,scope);
            return R.ok(fhMapWaters);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 筛选渔获
     */
    @UserLoginToken
    @ApiOperation("筛选渔获")
    @PostMapping("/screeningCatch")
    public R<List<FhIndexPostinfo>> screeningCatch(ScreeningCatchDto screeningCatchDto) {
        try {
            List<FhIndexPostinfo> fhIndexPostinfoList = iFhIndexPostinfoService.screeningCatchList(screeningCatchDto);
            return R.ok(fhIndexPostinfoList);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /**
     * 新增钓鱼点管理
     */
    @UserLoginToken
    @ApiOperation("添加钓鱼点")
    @PostMapping("/AddFishPoint")
    public R<String> add(FhInfoFishpoint fhInfoFishpoint) {
        try {
            fhInfoFishpointService.insertFhInfoFishpoint(fhInfoFishpoint);
            return R.ok("添加成功");
        } catch (Exception e) {
            return R.fail();
        }
    }


    /**
     * 查询钓鱼点列表
     */
    @UserLoginToken
    @ApiOperation("查询用户钓鱼点列表")
    @GetMapping("/userFishPointlist")
    public R<List<FhInfoFishpoint>> userList() {
        try {
            startPage();
            Long userId = LocalUserInfoUtil.getLocalUserInfo().getId();
            FhInfoFishpoint fhInfoFishpoint = new FhInfoFishpoint();
            fhInfoFishpoint.setUserId(userId);
            List<FhInfoFishpoint> list = fhInfoFishpointService.selectFhInfoFishpointList(fhInfoFishpoint);
            return R.ok(list);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询钓鱼点列表
     */
    @UserLoginToken
    @ApiOperation("查询所有钓鱼点列表")
    @GetMapping("/fishPointlist")
    public R<List<FhInfoFishpoint>> list() {
        try {
            FhInfoFishpoint fhInfoFishpoint = new FhInfoFishpoint();
            List<FhInfoFishpoint> list = fhInfoFishpointService.selectFhInfoFishpointList(fhInfoFishpoint);
            return R.ok(list);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询鱼类
     */
    @UserLoginToken
    @ApiOperation("查询所有鱼类")
    @GetMapping("/fishTypelist")
    public R<List<FhMapFishtype>> getFishTypeList(String name) {
        try {
            FhMapFishtype fhMapFishtype = new FhMapFishtype();
            fhMapFishtype.setFishType(name);
            List<FhMapFishtype> fhMapFishtypes = fhMapFishtypeService.selectFhMapFishtypeList(fhMapFishtype);
            return R.ok(fhMapFishtypes);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /*
     *  关注 取消 鱼类
     * */
    @UserLoginToken
    @ApiOperation("关注鱼类")
    @PostMapping ("/concernFishType")
    public R<String> concernFishType(@RequestBody FhMapFishtype fhMapFishtype) {
        try {
            fhMapFishtypeService.updateStatus(fhMapFishtype);
            return R.ok("操作成功");
        }catch (Exception e) {
                return R.fail(e.getMessage());
        }
    }



    /*
     *  关注 取消 鱼饵
     * */
    @UserLoginToken
    @ApiOperation("关注鱼饵")
    @PostMapping ("/concernFishbait")
    public R<String> concernFishType(@RequestBody FhMapFishbait fhMapFishbait) {
        try {
            fhMapFishbaitService.updateStatus(fhMapFishbait);
            return R.ok("操作成功");
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /*
     *  关注 取消 水域
     * */
    @UserLoginToken
    @ApiOperation("关注水域")
    @PostMapping ("/concernWater")
    public R<String> concernFishType(@RequestBody FhMapWater fhMapWater) {
        try {
            fhMapWaterService.updateStatus(fhMapWater);
            return R.ok("操作成功");
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    /**
     * 查询水域常见鱼种管理列表
     */
    @UserLoginToken
    @GetMapping("/watercommonfishList")
    public TableDataInfo list(FhMapWatercommonfish fhMapWatercommonfish)
    {
        startPage();
        List<FhMapWatercommonfish> list = fhMapWatercommonfishService.selectFhMapWatercommonfishList(fhMapWatercommonfish);
        return getDataTable(list);
    }


    /**
     * 查询鱼类管理列表
     */
    @UserLoginToken
    @GetMapping("/fishtypeList")
    public TableDataInfo list(FhMapFishtype fhMapFishtype)
    {
        startPage();
        List<FhMapFishtype> list = fhMapFishtypeService.selectFhMapFishtypeList(fhMapFishtype);
        return getDataTable(list);
    }


    /*
    *   水域筛选
    * */
    @UserLoginToken
    @GetMapping("/fhMapWaterList")
    public List<FhMapWater> fhMapWaterList(Integer timeRange,String month,String longitude,String latitude,String scope) {

        String[] monthArray = null;
        if (StringUtils.isNotBlank(month)){
            monthArray = month.split(",");
        }

        List<FhMapWater> list = fhMapWaterService.fhMapWaterList(timeRange,monthArray,longitude,latitude,scope);
        for (FhMapWater fhMapWater : list) {
            Long id = fhMapWater.getId();
            List<FhMapWatercommonfish> fhMapWatercommonfish = fhMapWatercommonfishService.getFhMapWatercommonfish(id);
            fhMapWater.setFhMapWatercommonfish(fhMapWatercommonfish);
        }
        return (list);
    }


    @ApiOperation("发送消息")
    @PostMapping("/sendApply")
    public String sendFriendApply(@RequestBody FhChatFriend artUserFriend,@RequestBody FhInviteRecord fhInviteRecord) throws IOException {

        /**
         * im替该用户向队长发送消息
         */
        Map<String, Object> body = new HashMap<>();
        body.put("SyncOtherMachine", 1);  //同步消息
        body.put("From_Account", artUserFriend.getUserId().toString());  // 发送者id
        body.put("To_Account", artUserFriend.getFriendId().toString());  // 接收者id
        body.put("MsgRandom", new RandomDataGenerator().nextLong(0, 4294967295L));  // 消息随机数
        // 消息体
        List<Map<String, Object>> MsgBody = new ArrayList<>();
        //消息内容
        Map<String, Object> Msg = new HashMap<>();
        Map<String, Object> MsgContent = new HashMap<>();

        Map<String, Object> Data = new HashMap<>();
        Data.put("businessID", "fish_invite");
        Data.put("data",artUserFriend);
        //保存发送邀请信息
        Data.put("inviteRecord",fhInviteRecord);
        // 将Map对象转换为JSON字符串
        String jsonString = JSON.toJSONString(Data);
        //封装消息到body中
        MsgContent.put("Data", jsonString);
        Msg.put("MsgType", "TIMCustomElem");
        Msg.put("MsgContent", MsgContent);
        //保存发送邀请信息
        Msg.put("inviteRecord",fhInviteRecord);
        MsgBody.add(Msg);
        body.put("MsgBody", MsgBody);

        return h5ArtUserFriendService.sendRequest("v4/openim/sendmsg", body);

    }


    //发送邀请
    @ApiOperation("发送邀请")
    @PostMapping("/sendInvite")
    public R<String> sendInvite(@RequestBody FhInviteRecord inviteRecord) throws IOException {
        try {
            //获取当前用户id
            Long userId = LocalUserInfoUtil.getLocalUserInfo().getId();
            //接收者id
            Long friendId = inviteRecord.getFriendId();

            // 获取当前时间戳(秒级)，保留7位
            long timestampPart = (System.currentTimeMillis() / 1000) % 10000000L;

            // 生成3位随机数
            long randomPart = ThreadLocalRandom.current().nextInt(100, 1000);

            // 组合成11位数字
            Long uuid =  timestampPart * 1000 + randomPart;
            inviteRecord.setId(uuid);


            FhChatFriend friend = new FhChatFriend();
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            this.sendFriendApply(friend,inviteRecord);

            fhInviteRecordServicel.sendInvite(inviteRecord);
            return R.ok("操作成功");
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }


    //是否同意邀请
    @ApiOperation("是否同意邀请")
    @PostMapping("/onAgreeInvite")
    public R<String> onAgreeInvite(@RequestBody FhInviteRecord inviteRecord) throws IOException {
        try {
            fhInviteRecordServicel.onAgreeInvite(inviteRecord);
            return R.ok("操作成功");
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }



}
